<template>
  <div class="app-container">
    <el-card>
      <el-row>
        <el-col :span="8" style="padding-right: 15px">
          <el-row>
            <el-col :span="18">
              <el-input
                v-model="userGroup"
                maxlength="100"
                placeholder="请输入用户组"
                clearable
                @input="getTreeList"
              />
            </el-col>
            <el-col :span="6">
              <el-row style="justify-content: center;">
                <el-button
                  link
                  type="success"
                  icon="Refresh"
                  @click="handleSynchronous"
                  :loading="loadingBtn"
                  style="padding-top: 10px"
                  >同步用户组</el-button
                >
              </el-row>
            </el-col>
          </el-row>
          <el-scrollbar :height="height" ref="scrollbarRef">
            <el-tree
              :data="treeData"
              :props="defaultProps"
              :default-expand-all="true"
              node-key="id"
              v-loading="treeLoading"
              :highlight-current="true"
              @current-change="handleTreeCheck"
              ref="treeRef"
            />
          </el-scrollbar>
        </el-col>
        <el-col
          :span="16"
          style="border-left: 3px #ddd solid; padding-left: 15px"
        >
          <p class="pinformation">搜索条件</p>
          <el-form
            :model="queryParams"
            ref="queryRef"
            label-width="80"
            @submit.native.prevent
          >
            <el-row>
              <el-col :span="12">
                <el-form-item label="用户名：" prop="firstName">
                  <el-input
                    v-model="queryParams.firstName"
                    maxlength="100"
                    placeholder="请输入用户名"
                    clearable
                    @keyup.enter="handleQuery"
                    :disabled="!queryParams.id"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="3">
                <el-row justify="end">
                  <el-button
                    type="success"
                    @click="handleQuery"
                    :disabled="!queryParams.id"
                    >搜索</el-button
                  >
                  <el-button
                    class="business resetBtn"
                    @click="resetQuery"
                    :disabled="!queryParams.id"
                    >重置</el-button
                  >
                </el-row>
              </el-col>
            </el-row>
          </el-form>
          <el-row>
            <p class="pinformation">用户列表</p>
            <p class="totalStyle">共 {{ total }} 条数据</p>
            <el-row style="padding-bottom: 5px; margin-left: 15px">
              <el-button
                link
                type="success"
                icon="Refresh"
                :disabled="!queryParams.id"
                @click="handleSynchronous1"
                :loading="loadingBtn1"
                >同步用户</el-button
              >
            </el-row>
          </el-row>
          <el-table
            ref="mainTableRef"
            class="business"
            v-loading="loading"
            :data="userTable"
          >
            <el-table-column label="用户名" align="center" prop="firstName" />
            <el-table-column label="用户组" align="center" prop="groupNames" />
          </el-table>
          <pagination
            v-show="total > 0"
            :total="total"
            v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize"
            @pagination="getList"
          />
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>

<script setup>
import { getUserGroup, listUser, postUser,getUser,postGroup,getGroup } from "@/api/flowable/flowUser";
import { ElMessageBox } from "element-plus";

const { proxy } = getCurrentInstance();
const queryParams = ref({ pageNum: 1, pageSize: 10 });
const userTable = ref([]);
const loading = ref(false);
const loadingBtn = ref(false);
const loadingBtn1 = ref(false);
const total = ref(0);
const height = ref("0px");
const treeData = ref([]);
const treeLoading = ref(false);
const userGroup = ref("");
const defaultProps = ref({
  label: "name",
  children: "childList",
});
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
function getTreeList() {
  treeLoading.value = true;
  getUserGroup({ name: userGroup.value }).then((res) => {
    treeData.value = res.data;
    treeLoading.value = false;
  });
}
function getList() {
  listUser(queryParams.value).then((res) => {
    userTable.value = res.rows;
    total.value = res.total;
  });
}
function handleSynchronous() {
  proxy.$modal
    .confirm("确认要同步用户组吗？")
    .then(() => {
      postGroup();
      loadingBtn.value = true;
      let timer = setInterval(() => {
        getGroup()
          .then((res) => {
            if (res.data == 2) {
              clearInterval(timer);
              loadingBtn.value = false;
              getTreeList();
              ElMessageBox.alert("同步成功", "提示", {
                type: "success",
              });
            }
          })
          .catch(() => {
            clearInterval(timer);
            loadingBtn.value = false;
            ElMessageBox.alert("同步失败", "提示", {
              type: "error",
            });
          });
      }, 5000);
    })
    .catch(() => {});
}
function handleSynchronous1() {
  proxy.$modal
    .confirm("确认要同步用户吗？")
    .then(() => {
      postUser();
      loadingBtn1.value = true;
      let timer = setInterval(() => {
        getUser()
          .then((res) => {
            if (res.data == 2) {
              clearInterval(timer);
              loadingBtn1.value = false;
              getList();
              ElMessageBox.alert("同步成功", "提示", {
                type: "success",
              });
            }
          })
          .catch(() => {
            clearInterval(timer);
            loadingBtn1.value = false;
            ElMessageBox.alert("同步失败", "提示", {
              type: "error",
            });
          });
      }, 5000);
    })
    .catch(() => {});
}

function handleTreeCheck(row, node) {
  if (row.id) {
    queryParams.value.id = row.id;
    getList();
  }
}

getTreeList();

watch(
  () => total.value,
  () => {
    if (total.value) {
      heightParams.value = [{ tableName: "main", bottomHeight: 73 }];
    } else {
      heightParams.value = [{ tableName: "main", bottomHeight: 21 }];
    }
    getTableHeight();
  }
);

function getHeight() {
  height.value = proxy.getSubFileHeight(proxy, "scrollbarRef", 21);
}
const heightParams = ref([{ tableName: "main", bottomHeight: 21 }]);
function getTableHeight() {
  proxy.adaptiveHeight(proxy, heightParams.value);
}
onMounted(() => {
  nextTick(function () {
    getTableHeight();
    window.addEventListener("resize", getTableHeight);
    getHeight();
    window.addEventListener("resize", getHeight);
  });
})
onUpdated(() => {
  nextTick(function () {
    getHeight();
    getTableHeight();
  });
});
onBeforeUnmount(() => {
  window.removeEventListener("resize", getHeight);
  window.removeEventListener("resize", getTableHeight);
});
</script>
